/*************************************************************
	MyTools
**************************************************************/
(function($){ myTools = {
	
	/****************************
	 * возвращает параметры обьекта 
	*****************************/
	myElementParams : function(elem) {
		var elem = ( typeof(elem) == "object" ) ? ( typeof(elem.jquery) == "undefined" ? elem : elem.get(0) ) : $(elem)[0];
		var left = 0;
		var top = 0;
		var width = elem.offsetWidth;
		var height = elem.offsetHeight;
		if (elem.offsetParent) {
			do {
					left += elem.offsetLeft;
					top += elem.offsetTop;
			} while (elem = elem.offsetParent);
		};
		if(self.innerWidth!=undefined) { A= [self.innerWidth,self.innerHeight];	}
		else {
			var D= document.documentElement;
			if(D) {A= [D.clientWidth,D.clientHeight];}
		}
		return {
			d: [width, height],
			p: [left, top]
		};
	},
	
	/***********************************
	 * дампер свойств\методов обьекта + кол-во
	 * @param {} obj
	 * @param {} obj_name
	 * @return {}
	************************************/
	objDump : function(obj, obj_name, ret) {
		var len = '';
		var result = '';
		for (var i in obj) {
			result += obj_name + '.' + i + ' = ' + obj[i] + '\n';
			len = ++len;
		}
		if (ret == true) {return result;}
		else {return len}
	}
}
})(jQuery);





/************************************************************
	Присутствие
*************************************************************/
$(function() {
	var which = $(".mP a");
	yTest = false;
	which.hover(
		function () { $(this).css('cursor','pointer'); },
		function () {}
	);
	which.click(function(){
		yTest = true;
		var obj = $(this);
		var cid = obj.attr('id');
		var Pid = obj.parent().parent().attr('id');
		var oldClass = obj.attr('class')
		var oldText = obj.text();

		if(boxerInplace2.inited == true) {
			boxerInplace2.hide();
			obj.removeAttr('class').addClass(oldClass);
		}
		/*var text = '<div class="owBlock" style=""><ol style="list-style: none;">'
			+ '<li class="mpl1" title="mpOut"><a href=""  class="mpOut" title="неявка">неявка</a></li>'
			+ '<li class="mpl2" title="mpDelay"><a href="#" class="mpDelay" title="опоздание">опоздание</a></li>'
			+ '<li class="mpl3" title="mpIll"><a href="" class="mpIll" title="болезнь">болезнь</a></li>'
			+ '<li class="mpl4" title="mpPass"><a href="" class="mpPass" title="пропуск">пропуск</a></li>'
			+ '<li class="mpl5" title="mpNone"><a href="" class="mpNone" title="">отмена</a></li>'
			+ '</div>';*/
		var text = '<div class="owBlock" style=""><ol style="list-style: none;">';
		for(var i in logEntryStatus) {
			text += '<li class="mpl1" title="'+ logEntryStatus[i][0] +'"><a href=""  class="'+ logEntryStatus[i][0] +'" title="'+ logEntryStatus[i][2] +'">'+ logEntryStatus[i][1] +'</a></li>';
		}
		text += '</div>';
		boxerInplace2.show(obj , text , {delay : false ,className : "confirm2", xId:'boxerInplace2'});
		
		var linkObj = $('.owBlock li a');
		linkObj.hover(function(){
			if (yTest === true ) {
				var xClass = $(this).parent().attr('title');
				var xText = $(this).attr('title');
				obj.removeAttr('class').addClass(xClass);
				obj.empty().html(xText)
				
				/* эвент на клик выбора */
				$(this).click(function(){
					if (oldText == xText) {
						obj.removeAttr('class').addClass(oldClass);
						obj.html(oldText);
						boxerInplace2.hide();
						return false;
					} else {
					obj.empty().removeAttr('class').addClass('ajaxWait');
					if( yTest == true) {
					yTest = false;
					myAjax.send({
						url: ajaxUrl,
						pid: Pid,
						type : 'presence',
						value : xText,
						succcess : function(){
							obj.removeAttr('class').addClass(xClass).html(xText);
						},
						error : function(){
							obj.removeAttr('class').addClass(oldClass);
							boxerInplace2.show(obj, '<div style="padding:10px; width:160px; text-align:center">Произошел сбой.<br />Попробуйте ещё раз</div>',
							{delay : 1500 ,className : "confirm2", xId:'boxerInplace2'});
						}
					});
					}
					boxerInplace2.hide();
					return false;
					}
				})
				/*overlay.click(function () {
					obj.removeAttr('class').addClass(oldClass);
					boxerInplace.hide();
					overlay.remove();
				});*/
			}
		},
		function(){
			if (yTest === true) {
				obj.removeAttr('class').addClass(oldClass);
				obj.html(oldText);
			}
		});
	});
});





/*************************************************************
 Marks
**************************************************************/
$(function() {
	var arrMarks = ['1-', '1', '1+', '2-', '2', '2+', '3-', '3', '3+', '4-', '4', '4+', '5-', '5', '5+'];
	
	var which = $('a.mmPlace');
	var xTest = false;
	
	if (!$.browser.msie) {
		which.hover(
			function () { $(this).addClass('mmHover');},
			function () { $(this).removeClass('mmHover'); }
		);
	}
	
	/* первый клик */
	which.bind("click" , function() {
		var obj = $(this);
		var cid = obj.attr('id');
		var Pid = obj.parent().parent().attr('id');
		var parentClass = obj.parent().parent().attr('class');
		var oldClass = obj.attr('class')
		var oldMark = obj.html();
		xTest = true;

		if(boxerInplace2.inited == true) {
			boxerInplace2.hide();
			obj.removeAttr('class').addClass(oldClass);
		}
		if (parentClass == 'mH') {
			var text = '<div class="owBlock" style=""><ol style="list-style: none;">'
				+ '<li class="mml5" title="mmFive"><a href="" class="mmHide" title="5-">5-</a><a href="" class="mpNone" title="5">5</a><a href="" class="mmHide" title="5+">5+</a></li>'
				+ '<li class="mml4" title="mmFor"><a href="" class="mmHide" title="4-">4-</a><a href="" class="mpPass" title="4">4</a><a href="" class="mmHide" title="4+">4+</a></li>'
				+ '<li class="mml3" title="mmThree"><a href="" class="mmHide" title="3-">3-</a><a href="" class="mpIll" title="3">3</a><a href="" class="mmHide" title="3+">3+</a></li>'
				+ '<li class="mml2" title="mmTwo"><a href="" class="mmHide" title="2-">2-</a><a href="" class="mm2" title="2">2</a><a href="" class="mmHide" title="2+">2+</a></li>'
				+ '<li class="mml1" title="mmOne"><a href="" class="mmHide" title="1-">1-</a><a href="" class="mm1" title="1">1</a><a href="" class="mmHide" title="1+">1+</a></li>'
				+ '</ol><div class="mmDelet"><a href="">удалить</a></div>'
				+ '</div>';
		} else if (parentClass == 'mZ'){
			var text = '<div class="owBlock" style=""><ol style="list-style: none;">'
				+ '<li class="mmExam true" title="mmExamTrue"><a href="#" title="зт">зачет</a></li>'
				+ '<li class="mmExam false" title="mmExamFalse"><a href="#" title="нзт">не зачет</a></li>'
				+ '</ol><div class="mmDelet"><a href="">удалить</a></div></div>';
		}
		boxerInplace2.show(obj , text , {delay : false ,className : "confirm2", xId:'boxerInplace3'});
			
		var linkObj2 = $('.owBlock li');
		/* ховер для видимости оценок */
		if( xTest === true && parentClass == 'mH') {
		linkObj2.hover(
			function(){
					var xMarkObj = $(this).find('a'); 
					xMarkObj.css('width','27px');
					var xHidden = $(this).find('a.mmHide');
					xHidden.css('display' , 'block');
			},
			function () {
					$(this).find('a').css('width','90px');
					xHidden = $(this).find('a.mmHide');
					xHidden.css('display' , 'none');
			}
		);
		}
		
		var linkObj = $('.owBlock li a');
		/* ховер выбора оценок */
		linkObj.hover(function() {
			if( xTest === true) {
				var xClass = $(this).parent().attr('title');
				var xText = $(this).attr('title');
				var xWindMarkVal = $(this).text();
					obj.text(xText);
				
				/* эвент для запуска */
				$(this).click(function(){
					if (xText == oldMark) {
						if (oldMark == '<span>x</span>') {obj.html(oldMark);obj.removeAttr('class').addClass('mmPlace');}
						else {obj.html(oldMark);}
						boxerInplace2.hide();
						return false;
					} else {
					obj.removeAttr('class').addClass(xClass);
					obj.empty().addClass('ajaxWait2');
					if( xTest == true) {
						xTest = false;
						myAjax.send({
						pid: Pid,
						type : 'mark',
						value : xText,
						succcess : function(){
							obj.empty().removeClass('ajaxWait2');
							obj.text(xText);
						},
						error : function(){
							obj.removeClass('ajaxWait2');
							if (oldMark == '<span>x</span>') {obj.html(oldMark);obj.removeAttr('class').addClass('mmPlace');}
							else {obj.html(oldMark);}
							boxerInplace2.show(obj,
								'<div style="padding:10px; width:160px; text-align:center">Произошел сбой.<br />Попробуйте ещё раз</div>',
								{delay : 2000 ,className : "confirm2", xId:'boxerInplace2'}
							);
						}
					})
					}
					boxerInplace2.hide();
					return false;
				}
				})
			}
		},
		function(){
			if( xTest === true) {
				obj.html(oldMark);
		}});
		/* эвент для удаления отметки */
		$('div .mmDelet').click(function() {
			if (oldMark == '<span>x</span>') {
				obj.html('<span>x</span>');
				obj.removeAttr('class').addClass('mmPlace');
			} else {
				obj.empty().addClass('ajaxWait2');
				if( xTest == true) {
				xTest = false;
				myAjax.send({
					pid: Pid,
					type : 'mark',
					value : 'delet',
					succcess : function(){
						obj.html('<span>x</span>').removeAttr('class').addClass('mmPlace');
					},
					error : function(){
						obj.removeClass('ajaxWait2');
						if (oldMark == '<span>x</span>') {obj.html(oldMark);obj.removeAttr('class').addClass('mmPlace');}
						else {obj.html(oldMark);}
						boxerInplace2.show(obj,
							'<div style="padding:10px; width:160px; text-align:center">Произошел сбой.<br />Попробуйте ещё раз</div>',
							{delay : 2000 ,className : "confirm2", xId:'boxerInplace2'}
						);
					}
				});
				}
			}
			boxerInplace2.hide();
			return false;
		});
		return false;
	});
	
});
	



/*********************************************************
	My AJAX
**********************************************************/
(function($){ myAjax = {
	Options : {
		timeout: 10000,
		simpleAjax: false,
		urlDispos: false,
		url: 'ajax.php'
	},
	
	send : function(options) {
		this.options = {};
		$.extend( this.options, this.Options, options );
		myAjax.Options = this.options;
//		myAjax.Options.urlDispos = false? url=this.options.url : url=myAjax.Options.url;
		myOverlay.show();
		$.ajax({
			timeout : myAjax.Options.timeout,
			type : "POST",
			url : this.options.url,
			dataType : "json",
//			ifModified : true,
			data: {'lessonID':lessonID, 'Pid':this.options.pid , 'type':this.options.type, 'value':this.options.value},
			success : function (data) {
				myOverlay.hide();
				myAjax.Options.succcess();
			},
			error : function () {
				myOverlay.hide();
				myAjax.Options.error();
			}
		});	
	},
	
	response : function() {
		$('.test1').fadeOut(1000, function(){alert('OK');});
	}
};
})(jQuery);




/****************************************************
	MyOverlay
*****************************************************/
(function($){ myOverlay = {
	options : {
		zIndex : 1000,
		overlayId : 'overlay',
		destroy : true
	},
	show : function () {
		options = myOverlay.options;
		if (options.destroy == false && this.inited == true) {
			$("#"+overlayId).show();
		} else {
			$('body').append('<div id="'+options.overlayId+'" style="z-index:'+options.zIndex+';"></div>');
		}
		this.inited = true;
	},
	hide : function () {
		if (options.destroy == false) {
			$('#'+options.overlayId).hide();
		} else {
			$('#'+options.overlayId).remove();
			this.inited = false;
		}
	}
}
})(jQuery);





/******************************************* 
	im.boxerInplace2
*******************************************/
(function($) {
boxerInplace2 = {
	nativeOptions : {
		buttons : [],
		delay : 2000
	},
	show: function (element, text,  options ) {
		var element = ( typeof(element) == "object" ) ? ( typeof(element.jquery) == "undefined" ? element : element.get(0) ) : $(element)[0];
		this.element = element;
		this.options = {};
		$.extend( this.options, this.nativeOptions, options );
		var params = this.elementParams( element );
		
		this.zId = (this.options.xId != '') ? this.options.xId : 'boxerInplace';
		
		this.init(); // вызов INIT
		this.html.text.html( text ); // подставляем текст
		
		/* кнопачки */
		this.html.buttons.html("").hide();
		if(this.options.buttons.length) { this.html.buttons.show(); }
		$.each(this.options.buttons, function () {
			$('<a href="#" title="' + this.text + '">' + this.text + '</a>').appendTo(boxerInplace2.html.buttons).click( function () {
				boxerInplace2.hide();
				this.callback.apply( boxerInplace2.element );
				return false;
			})[1].callback = this.callback || function () {} ;
		});
		
		this.html.css( {
			left: params.p[0],
			top: params.p[1] + params.d[1] + 5
		});
		this.html[0].className = this.options.className || ""; // делаем классно
		if (this.timeToClose) { clearTimeout(this.timeToClose); };
		this.html.hide().fadeIn(200);
		if (this.options.delay) { this.timeToClose = setTimeout( function () { boxerInplace2.hide(); }, this.options.delay ); };
		
		/*this.overlay.click(function() {
			this.overlay.hide();
			this.html.hide();
		});*/
		
	},
	hide: function () {
		//this.html.fadeOut(200);
		this.html.fadeOut(200 , function(){ $(this).remove(); });
		this.inited = false;
	},
	init: function () {
		if (this.inited) { return false; };
		
		this.html = $('<div id="'+ this.zId +'"><div class="text"></div><div class="btns"></div></div>')
			.appendTo($("body")).hide();
//		this.html = $('<div id="boxerInplace2"><div class="text"></div><div class="btns"></div></div>').appendTo($("body")).hide();
		this.html.text = this.html.find(".text");
		this.html.buttons = this.html.find(".btns");
		
		this.inited = true;
	},
	elementParams: function ( element ) {
		var left = 0;
		var top = 0;
		var width = element.offsetWidth;
		var height = element.offsetHeight;
		if (element.offsetParent) {
			do {
					left += element.offsetLeft;
					top += element.offsetTop;
			} while (element = element.offsetParent);
		};
		
		var A;
		if(self.innerWidth!=undefined) { A= [self.innerWidth,self.innerHeight];	}
		else {
			var D= document.documentElement;
			if(D) {A= [D.clientWidth,D.clientHeight];}
		}
		if (Math.round(A[0]) < 1190) { left = left-(left-Math.round(A[0])+200);}
		return {
			d: [width, height],
			p: [left, top]
		};
	}
};

window.boxerInplaceMessage = function(element, text, delay) {
	boxerInplace2.show( element, text, { delay : delay } );
};

window.boxerInplaceError = function(element, text, delay) {
	boxerInplace2.show( element, text, { className : "error", delay : delay } );
};

window.boxerInplaceConfirm = function( element, text, yescallback, nocallback, yestext, notext ) {
	boxerInplace2.show( element, text, { 
		buttons: [
			{ text : yestext || "Да",
			  callback : yescallback
			},
			{ text : notext || "Нет",
			  callback : nocallback
			}
		],
		delay : false,
		className : "confirm"
	});
};

$.fn.boxerInplaceMessage = function(text, delay) {
	return this.each ( function () {
		$(this).click( function () {
			boxerInplaceMessage(this, this.boxerInplace2.text, this.boxerInplace2.delay);
			return false;
		});
		this.boxerInplace2 = {
			text : text,
			delay : delay
		};
	});
};

$.fn.boxerInplaceError = function(text, delay) {
	return this.each ( function () {
		$(this).click( function () {
			boxerInplaceError(this, this.boxerInplace2.text, this.boxerInplace2.delay);
			return false;
		});
		this.boxerInplace2 = {
			text : text,
			delay : delay
		};
	});
};

$.fn.boxerInplaceConfirm = function(text, yescallback, nocallback, yestext, notext) {
	return this.each ( function () {
		$(this).click( function () {
			boxerInplaceConfirm(this, this.boxerInplace2.text, this.boxerInplace2.yescallback, this.boxerInplace2.nocallback, this.boxerInplace2.yestext, this.boxerInplace2.notext);
			return false;			
		});
		if ( ( typeof(yescallback) == "undefined" ) && ( typeof(this.href) != "undefined" ) ) { 
			yescallback = function () {
				window.location = this.href;
			};
		};		
		this.boxerInplace = {
			text : text,
			yescallback : yescallback, 
			nocallback : nocallback,
			yestext : yestext, 
			notext : notext
		};
	});
};
})(jQuery);



/* ******************************************************** */
(function($){ imMarks = {
	
	init : function(options) {
		var t = options.type;
		var w = options.where;
		var a = options.markArr;
		$(w+' td.'+t+' a').bind('click', function() {
			$(this).hide();
			return false;
		}
		);
	},
	
	objectTest : function(e) {
		var elem = ( typeof(e) == "object" ) ? ( typeof(e.jquery) == "undefined" ? e : e.get(0) ) : $(e)[0];
		return elem;
	}
}
})(jQuery);

